﻿using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using WebAnalyser.UI;

namespace WebAnalyser.Library
{
    public static class Settings
    {
        private const string Path = @".\WebAnalyser.ini";
        [SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("kernel32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int GetPrivateProfileString(
            string lpAppName,
            string lpKeyName,
            string lpDefault,
            StringBuilder lpReturnedString,
            int nSize,
            string lpFileName);
        [SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("kernel32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int WritePrivateProfileString(
            string lpAppName,
            string lpKeyName,
            string lpString,
            string lpFileName);

        public static string ReadValue(string section, string key)
        {
            StringBuilder result = new StringBuilder(1024);
            int i = GetPrivateProfileString(section, key, null, result, 1024, Path);
            if (i == 0)
            {
                return null;
            }
            return result.ToString();
        }

        public static void WriteValue(string section, string key, string value)
        {
            int i = WritePrivateProfileString(section, key, value, Path);
            if (i == 0)
            {
                DialogBox.Error("An error occured while writing in the configuration file.", "WebAnalyser");
            }
        }

        /*public List<string> GetSections()
        {
            string resultStr = new string(' ', 65536);
            GetPrivateProfileString(null, null, null, resultStr, 65536, this._path);
            List<string> resultList = new List<string>(resultStr.Split('\0'));
            resultList.RemoveRange(resultList.Count - 2, 2);
            return resultList;
        }

        public List<string> GetKeys(string section)
        {
            string resultStr = new string(' ', 32768);
            GetPrivateProfileString(section, null, null, resultStr, 32768, this._path);
            List<string> resultList = new List<string>(resultStr.Split('\0'));
            resultList.RemoveRange(resultList.Count - 2, 2);
            return resultList;
        }*/
    }
}